const IOUtils={};

(() => {
	IOUtils.createURL= function(file){
		var url = null;
		if (window.createObjectURL != undefined) {
			url = window.createObjectURL(file);
		} else if (window.URL != undefined) { // mozilla(firefox)  
			url = window.URL.createObjectURL(file);
		} else if (window.webkitURL != undefined) { // webkit or chrome  
			url = window.webkitURL.createObjectURL(file);
		}
		return url;
	};

	function add0(m){return m<10?('0'+m):m }
	
	IOUtils.getFileName=function(file){
			var pos =Math.max(file.lastIndexOf("\\"),file.lastIndexOf("/"));
			var filename = file.substring(pos+1);
			return filename.lastIndexOf(".")>0? filename.substring(0,filename.lastIndexOf(".")):filename;
			//var fileextname = filename.substring(filename.lastIndexOf("."), filename.length);
	};
	IOUtils.getSaveFileName=function(baseFileName,ext)
	{
		var time = new Date();
		var y = time.getFullYear();
		var m = time.getMonth()+1;
		var d = time.getDate();
		var h = time.getHours();
		var mm = time.getMinutes();
		var s = time.getSeconds();
		return baseFileName+'_'+y+add0(m)+add0(d)+'_'+add0(h)+add0(mm)+add0(s)+ext;
	}
	IOUtils.downLoad=function(url,baseFileName,ext,appendTime=true){
		var oA = document.createElement("a");
		oA.download =appendTime? IOUtils.getSaveFileName(baseFileName,ext):(baseFileName+ext);// 设置下载的文件名，默认是'下载'
		oA.href = url;
		document.body.appendChild(oA);
		oA.click();
		oA.remove(); // 下载之后把创建的元素删除
	}
	IOUtils.guid=function() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
			var r = Math.random() * 16 | 0,
				v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		});
	}
})();
